<?php 
class   Country_FieldController extends Core_Controller_Action_Standard{
    
    public function init()
    {
        $headStyle = new Zend_View_Helper_HeadLink();
        if (Zend_Registry::isRegistered('StaticBaseUrl')) {
          $headStyle->prependStylesheet(Zend_Registry::get('StaticBaseUrl')
                  . 'application/modules/Core/externals/styles/admin/main.css');
        } 

        $viewer = Engine_Api::_()->user()->getViewer();
        if (!$viewer || empty( $viewer->user_id ) ) {
            return $this->_forward('notfound');
        }
        
        $country_Id = $this->_getParam('country_id',$this->_getParam('cid',0));
        $managerT = Engine_Api::_()->getDbtable('managers','country');
        $isAdmin = $managerT -> allowAddSystemField($viewer->user_id);
        if (!$isAdmin) {
           return $this->_forward('notfound');
        }
    }
    
    public function indexAction(){
        
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('country_admin_main', array(), 'country_admin_main_field');
       
        $table = Engine_Api::_()->getDbTable('fields', 'country');
        $select = $table->select();
        $this->view->paginator = $paginator = Zend_Paginator::factory($select);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(10);
        $this->view->paginator = $paginator->setCurrentPageNumber($page);
    }
    
   public function editAction(){
    
        $request = $this->getRequest();
        $field_id = $request->getParam('id',0);
      	$field = Engine_Api::_()->getItem('country_field', $field_id);
        if(!$field) return;
        //$values = $field->toArray();
        $values = array();
        $values['field_title'] = $field->title;
        $values['description_field'] = $field->description;
        $values['parent_field'] = $field->parent_id;
        
        //set form values
      	$this->view->form = $form = new Country_Form_Admin_Field_Create();
        $form->setTitle('Edit Field');
      	$form->populate($values);
        
         if($request->isPost()){
            
            if(!$form->isValid($request->getPost())) return;
            try{
                $values = $form->getValues();
            
                
                $field->title = $values['field_title'];
                $field->description = $values['description_field'];
                $field->parent_id = $values['parent_field'];
                $field->modified_date = date("Y-m-d H:i:s");
                
                $field->save();
                
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Edittig Successfully.'))
            	));
            }
            catch(Exception $e){
                ///
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Edittig Error.'))
            	));
            }
            
            
        }
        
   } 
   public function deleteAction()
    {
        
        $request = $this->getRequest();
        $field_id = $request->getParam('id',0);
        $field = Engine_Api::_()->getItem('country_field', $field_id);
        $this->view->form = $form = new Country_Form_Admin_Field_Delete();
        $form->setDescription('Could You want to delete this field? If you delete this field, information in country will be delete if those relateion to this field and it is can not recover');
        if($request->isPost())
        {
            if(!$form->isValid($request->getPost())) return;
            $table = Engine_Api::_()->getDbtable('countryfields','country');
            $fieldT = Engine_Api::_()->getDbtable('fields','country');
            
            $db = $table->getAdapter();
            $db->beginTransaction();
            try {
                 
                 $childrens = $fieldT->getChildrenId($field_id);
                 $childrens[] = $field_id;
                 $table->delete(array(
                        'field_id in (?)' => $childrens
                    )
                 );
                 $fieldT->delete(array(
                        'parent_id = ?' => $field_id
                    )
                 );
                 $field->delete();
                 $db->commit();
                 $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Successful.'))
            	));
            } catch(Exception $e){
                $db->rollBack();
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'messages' => array($this->view->translate('There is error when delete this field, pleaes contact admin.'))
            	));
                
            }
        
            
           
            
        }
    }
    public function addAction(){

        
        $request = $this->getRequest();
      	$this->view->form = $form = new Country_Form_Admin_Field_Create();
        $form->setTitle('Create New Field');
        
        
        if($request->isPost()){
            
            if(!$form->isValid($request->getPost())) return;
            try{
                $values = $form->getValues();
            
                $fieldTb  = Engine_Api::_()->getDbTable('fields', 'country');
                
                $new_field = $fieldTb->createRow();
                
                $new_field->title = $values['field_title'];
                $new_field->description = $values['description_field'];
                $new_field->parent_id = $values['parent_field'];
                $new_field->creation_date = date("Y-m-d H:i:s");
                $new_field->modified_date = date("Y-m-d H:i:s");
                
                $new_field->save();
                
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Successfully.'))
            	));
            }
            catch(Exception $e){
                ///
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Error.'))
            	));
            }
            
            
        }
    }
}
?>